package com.iscc.fake.controller;

import com.google.gson.Gson;
import com.iscc.fake.domain.User;
import com.iscc.fake.service.Impl.UserServiceImpl;
import com.iscc.fake.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet({"/register.do","/login.do","/checkAccount.do","/gerenzhongxin.do","/updateInfo.do","/exit.do","/updatePwd.do"})
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String uri = request.getRequestURI();//URI目标为 /fake/register.do；
        String action = uri.substring(uri.lastIndexOf("/") + 1);
        PrintWriter out = response.getWriter();
        UserService userService = new UserServiceImpl();

        Map<String, Object> resultMap = new HashMap<>();
        //转化为Json
        Gson gson = new Gson();
        if ("register.do".equals(action)) {
            //执行注册
            String account = request.getParameter("account");
            String password = request.getParameter("password");

            User user = new User(account, password);
            int result = userService.addUser(user);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "注册成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "注册失败");
            }

        }
        if ("login.do".equals(action)) {
            String account = request.getParameter("account");
            String password = request.getParameter("password");
            User user = userService.login(account);
            if (user == null) {
                resultMap.put("code", "202");
                resultMap.put("msg", "账户不存在");
            } else if (!user.getPassword().equals(password)) {
                resultMap.put("code", "201");
                resultMap.put("msg", "密码输入有误");
            } else if(user.getPower() != "1"){
                session.setAttribute("user",user);
                resultMap.put("code", "200");
                resultMap.put("msg", "登录成功");
                resultMap.put("result",user);
            }else {
                session.setAttribute("user",user);
                resultMap.put("code", "205");
                resultMap.put("msg", "管理员登录成功");
                resultMap.put("result",user);
            }

        }else if("checkAccount.do".equals(action)){
            String account = request.getParameter("account");
            User user = userService.login(account);
            if(user != null){
                resultMap.put("code", "201");
                resultMap.put("message", "账户已存在");
            }else{
                resultMap.put("code", "200");
                resultMap.put("message", "账户可用");
            }
        }else if("gerenzhongxin.do".equals(action)){
            // 获得获得对象
            User user = new User();
            // 获得userService对象
            userService = new UserServiceImpl();
            // 获得存入session中的uno
            int uno1 = ((User)session.getAttribute("user")).getUno();
            // 将uno存进去，返回一个user对象
            user = userService.gerenzhongxinService(uno1);
            // 已经获得user所有信息，将信息存到json中
            resultMap.put("uno",user.getUno()); // 用户编号
            resultMap.put("account",user.getAccount());
            resultMap.put("password",user.getPassword()); // 用户密码
            resultMap.put("gender",user.getSex()); // 性别
            resultMap.put("birthday",user.getBirthday()); // 生日
            resultMap.put("email",user.getEmail()); // 邮箱
            resultMap.put("phone",user.getPhone()); // 手机号
            resultMap.put("uname",user.getUname()); // 昵称
            resultMap.put("status",user.getStatus()); // 状态 封禁 或是 正常
            resultMap.put("power",user.getPower()); // 身份 普通用户 还是 管理员// 头像
            resultMap.put("realname",user.getRealname());
        }else if("updateInfo.do".equals(action)){
            // 创建user对象
            User user = new User();
            // 获得页面参数并存入user对象
            user.setUno(Integer.parseInt(request.getParameter("uno")));
            user.setUname(request.getParameter("uname"));
            user.setSex(request.getParameter("gender"));
            user.setBirthday(request.getParameter("birthday"));
            user.setEmail(request.getParameter("email"));
            user.setPhone(request.getParameter("phone"));
            user.setRealname(request.getParameter("realname"));
            user.setStatus(request.getParameter("status"));
            user.setPower(request.getParameter("power"));



            // 调用service 的方法 将 user对象扔进去
            // 返回受影响行数
            int rs = userService.updateInfoService(user);

            if(rs == -1){
                resultMap.put("code",201); // 修改失败
            } else{
                resultMap.put("code",200); // 修改成功
            }
    } else if("exit.do".equals(action)){  // 前端点击退出按钮 清空session中的用户名

            // 清空session
            session.removeAttribute("user");
            System.out.println(session.getAttribute("user"));
            if(session.getAttribute("user") == null){
                resultMap.put("code","200");
            }



        } else if("updatePwd.do".equals(action)){ // 修改密码 通过uno修改密码

            // 获得uno 和 新密码
            int uno = Integer.parseInt(request.getParameter("uno"));
            String pwd = request.getParameter("password");

            // 传入新密码 和 uno 并获得受影响行数
            int rs = userService.updatePwd(pwd,uno);

            if(uno == 1) {
                resultMap.put("code",200 );
                resultMap.put("msg","修改成功");
            } else {
                resultMap.put("code",201 );
                resultMap.put("msg","修改失败");
            }
            // 转换json 并发送给前端
            out.print(gson.toJson(resultMap));



        }
        String str = gson.toJson(resultMap);
        out.print(str);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}